<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Excel文件上传系统</title>
    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }
        
        .header p {
            font-size: 1.1em;
            opacity: 0.9;
        }
        
        .main-content {
            padding: 40px;
        }
        
        .upload-section {
            margin-bottom: 40px;
        }
        
        .upload-area {
            border: 3px dashed #ddd;
            border-radius: 10px;
            padding: 40px;
            text-align: center;
            transition: all 0.3s ease;
            cursor: pointer;
            margin-bottom: 30px;
        }
        
        .upload-area:hover {
            border-color: #4facfe;
            background-color: #f8f9ff;
        }
        
        .upload-area.dragover {
            border-color: #4facfe;
            background-color: #e3f2fd;
            transform: scale(1.02);
        }
        
        .upload-icon {
            font-size: 4em;
            color: #4facfe;
            margin-bottom: 20px;
        }
        
        .upload-text {
            font-size: 1.2em;
            color: #666;
            margin-bottom: 15px;
        }
        
        .file-input {
            display: none;
        }
        
        .upload-btn {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 25px;
            font-size: 1.1em;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(79, 172, 254, 0.4);
        }
        
        .upload-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(79, 172, 254, 0.6);
        }
        
        .upload-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        
        .progress-container {
            margin: 20px 0;
            display: none;
        }
        
        .progress-bar {
            width: 100%;
            height: 8px;
            background-color: #f0f0f0;
            border-radius: 4px;
            overflow: hidden;
            position: relative;
        }
        
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #4facfe, #00f2fe);
            width: 0%;
            transition: width 0.3s ease;
        }
        
        .progress-text {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: #333;
            font-weight: bold;
            font-size: 0.9em;
            text-shadow: 0 0 3px rgba(255,255,255,0.8);
        }
        
        .progress-info {
            display: flex;
            justify-content: space-between;
            margin-top: 10px;
            font-size: 0.9em;
            color: #666;
        }
        
        .estimated-time {
            color: #4facfe;
            font-weight: bold;
        }
        
        .result-section {
            margin-top: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 10px;
            display: none;
        }
        
        .result-title {
            font-size: 1.3em;
            color: #333;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
        }
        
        .result-title .icon {
            margin-right: 10px;
            font-size: 1.2em;
        }
        
        .success .icon {
            color: #28a745;
        }
        
        .error .icon {
            color: #dc3545;
        }
        
        .result-details {
            background: white;
            padding: 15px;
            border-radius: 8px;
            border-left: 4px solid #4facfe;
        }
        
        .search-section {
            margin-top: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 10px;
        }
        
        .search-title {
            font-size: 1.3em;
            color: #333;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
        }
        
        .search-title .icon {
            margin-right: 10px;
            color: #4facfe;
        }
        
        .search-form {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
        }
        
        .search-input {
            flex: 1;
            padding: 12px 15px;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 1em;
            transition: border-color 0.3s ease;
        }
        
        .search-input:focus {
            outline: none;
            border-color: #4facfe;
        }
        
        .search-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 8px;
            font-size: 1em;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .search-btn:hover {
            transform: translateY(-1px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        
        .search-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        
        .price-display {
            background: white;
            padding: 20px;
            border-radius: 8px;
            border-left: 4px solid #28a745;
        }
        
        .price-info {
            font-size: 1.2em;
            color: #333;
        }
        
        .price-value {
            font-size: 1.5em;
            color: #28a745;
            font-weight: bold;
            margin-top: 10px;
        }
        
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #4facfe;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-right: 10px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .error-message {
            color: #dc3545;
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            padding: 10px;
            border-radius: 5px;
            margin-top: 10px;
        }
        
        /* 成功弹窗样式 */
        .success-stat {
            padding: 15px;
            border-radius: 8px;
            background-color: #f8f9fa;
        }
        
        .success-stat .stat-number {
            font-size: 2em;
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .success-stat .stat-label {
            color: #666;
            font-size: 0.9em;
        }
        
        /* 模态框样式 */
        .modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            display: none;
            z-index: 1000;
            justify-content: center;
            align-items: center;
        }
        
        .modal {
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            max-width: 500px;
            width: 90%;
            max-height: 90vh;
            overflow-y: auto;
        }
        
        .modal-header {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
            color: white;
            padding: 20px;
            border-radius: 10px 10px 0 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .modal-title {
            margin: 0;
            font-size: 1.2em;
        }
        
        .modal-close {
            background: none;
            border: none;
            color: white;
            font-size: 1.5em;
            cursor: pointer;
            padding: 0;
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            transition: background-color 0.3s;
        }
        
        .modal-close:hover {
            background-color: rgba(255, 255, 255, 0.2);
        }
        
        .modal-body {
            padding: 20px;
        }
        
        .modal-footer {
            padding: 20px;
            border-top: 1px solid #eee;
            text-align: right;
        }
        
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1em;
            transition: all 0.3s ease;
        }
        
        .btn-success {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
            color: white;
        }
        
        .btn-success:hover {
            transform: translateY(-1px);
            box-shadow: 0 5px 15px rgba(40, 167, 69, 0.4);
        }
        
        .upload-stats {
            display: flex;
            gap: 20px;
            margin: 20px 0;
            justify-content: center;
            flex-wrap: wrap;
        }
        
        .stat-item {
            text-align: center;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 8px;
            min-width: 120px;
        }
        
        .stat-number {
            font-size: 1.8em;
            font-weight: bold;
            color: #4facfe;
        }
        
        .stat-label {
            color: #666;
            font-size: 0.9em;
            margin-top: 5px;
        }
        
        /* 图标样式 */
        .icon {
            display: inline-block;
            width: 1em;
            height: 1em;
            vertical-align: middle;
            margin-right: 0.5em;
        }
        
        .icon-upload::before {
            content: "📁";
        }
        
        .icon-search::before {
            content: "🔍";
        }
        
        .icon-success::before {
            content: "✅";
        }
        
        .icon-error::before {
            content: "❌";
        }
        
        .icon-spinner::before {
            content: "⏳";
        }
        
        .icon-excel::before {
            content: "📊";
        }
        
        .icon-close::before {
            content: "✕";
        }
        
        .icon-check::before {
            content: "✓";
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                margin: 10px;
                border-radius: 10px;
            }
            
            .main-content {
                padding: 20px;
            }
            
            .header h1 {
                font-size: 2em;
            }
            
            .search-form {
                flex-direction: column;
            }
            
            .upload-stats {
                flex-direction: column;
                align-items: center;
            }
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="container">
            <div class="header">
                <h1><span class="icon icon-excel"></span> Excel文件上传系统</h1>
                <p>上传xlsx文件，数据将保存到MySQL数据库中</p>
            </div>
            
            <div class="main-content">
                <!-- 上传区域 -->
                <div class="upload-section">
                    <div class="upload-area" id="uploadArea">
                        <div class="upload-icon">
                            <span class="icon icon-upload"></span>
                        </div>
                        <div class="upload-text">点击选择文件或拖拽文件到此处</div>
                        <div style="margin: 10px 0; color: #999;">支持 .xlsx 格式</div>
                        <input type="file" id="fileInput" class="file-input" accept=".xlsx,.xls">
                        <button class="upload-btn" id="uploadBtn">
                            <span class="icon icon-upload"></span> 选择文件
                        </button>
                    </div>
                    
                    <!-- 进度条和预计时间 -->
                    <div class="progress-container" id="progressContainer">
                        <div class="progress-bar">
                            <div class="progress-fill" id="progressFill"></div>
                            <div class="progress-text" id="progressText">
                                <span class="icon icon-spinner"></span> 
                                上传中... 0%
                            </div>
                        </div>
                        <div class="progress-info">
                            <span id="progressInfo">已处理: 0/0</span>
                            <span class="estimated-time" id="estimatedTime"></span>
                        </div>
                    </div>
                    
                    <div class="result-section" id="resultSection">
                        <div class="result-title" id="resultTitle">
                            <span class="icon icon-success"></span>
                            <span id="resultText"></span>
                        </div>
                        <div class="result-details" id="resultDetails"></div>
                    </div>
                </div>
                
                <!-- 检索功能 -->
                <div class="search-section">
                    <div class="search-title">
                        <span class="icon icon-search"></span>
                        根据订单编号检索数据
                    </div>
                    <div class="search-form">
                        <input type="text" 
                               class="search-input" 
                               id="searchInput"
                               placeholder="请输入订单编号">
                        <button class="search-btn" id="searchBtn">
                            <span class="icon icon-search"></span> 检索
                        </button>
                    </div>
                    <div class="price-display" id="priceDisplay" style="display: none;">
                        <div class="price-info">订单信息：</div>
                        <div id="searchResultContent"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 成功弹窗 -->
    <div class="modal-overlay" id="successModal">
        <div class="modal">
            <div class="modal-header">
                <h5 class="modal-title">
                    <span class="icon icon-success"></span> 上传成功！
                </h5>
                <button type="button" class="modal-close" onclick="closeModal()">
                    <span class="icon icon-close"></span>
                </button>
            </div>
            <div class="modal-body" id="modalBody">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" onclick="closeModal()">
                    <span class="icon icon-check"></span> 确定
                </button>
            </div>
        </div>
    </div>

    <script>
        // 全局变量
        let isUploading = false;
        let isSearching = false;
        let uploadProgress = 0;
        let processedCount = 0;
        let totalCount = 0;
        let estimatedTime = '';
        let uploadStartTime = 0;

        // DOM 元素
        const uploadArea = document.getElementById('uploadArea');
        const fileInput = document.getElementById('fileInput');
        const uploadBtn = document.getElementById('uploadBtn');
        const progressContainer = document.getElementById('progressContainer');
        const progressFill = document.getElementById('progressFill');
        const progressText = document.getElementById('progressText');
        const progressInfo = document.getElementById('progressInfo');
        const estimatedTimeEl = document.getElementById('estimatedTime');
        const resultSection = document.getElementById('resultSection');
        const resultTitle = document.getElementById('resultTitle');
        const resultText = document.getElementById('resultText');
        const resultDetails = document.getElementById('resultDetails');
        const searchInput = document.getElementById('searchInput');
        const searchBtn = document.getElementById('searchBtn');
        const priceDisplay = document.getElementById('priceDisplay');
        const searchResultContent = document.getElementById('searchResultContent');
        const modalBody = document.getElementById('modalBody');
        const successModal = document.getElementById('successModal');

        // 初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 绑定事件
            uploadBtn.addEventListener('click', selectFile);
            fileInput.addEventListener('change', handleFileSelect);
            searchBtn.addEventListener('click', searchOrder);
            searchInput.addEventListener('keyup', function(e) {
                if (e.key === 'Enter') {
                    searchOrder();
                }
            });

            // 拖拽事件
            uploadArea.addEventListener('dragover', function(e) {
                e.preventDefault();
                uploadArea.classList.add('dragover');
            });

            uploadArea.addEventListener('dragleave', function() {
                uploadArea.classList.remove('dragover');
            });

            uploadArea.addEventListener('drop', function(e) {
                e.preventDefault();
                uploadArea.classList.remove('dragover');
                const files = e.dataTransfer.files;
                if (files.length > 0) {
                    handleFile(files[0]);
                }
            });

            // 点击模态框外部关闭
            successModal.addEventListener('click', function(e) {
                if (e.target === successModal) {
                    closeModal();
                }
            });
        });

        function selectFile() {
            fileInput.click();
        }

        function handleFileSelect(event) {
            const file = event.target.files[0];
            if (file) {
                handleFile(file);
            }
        }

        function handleFile(file) {
            if (!file.name.endsWith('.xlsx') && !file.name.endsWith('.xls')) {
                showUploadResult('error', '请选择Excel文件（.xlsx或.xls格式）');
                return;
            }
            uploadFile(file);
        }

        async function uploadFile(file) {
            isUploading = true;
            uploadProgress = 0;
            processedCount = 0;
            totalCount = 0;
            estimatedTime = '';
            hideUploadResult();
            uploadStartTime = Date.now();

            // 更新UI状态
            updateUploadButtonState();
            showProgress();
            updateProgress(0, 0, 0);

            const formData = new FormData();
            formData.append('excel', file);

            try {
                const response = await fetch('/api/upload-excel-batch', {
                    method: 'POST',
                    body: formData
                });

                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }

                const data = await response.json();
                
                isUploading = false;
                uploadProgress = 100;
                updateProgress(100, processedCount, totalCount);

                if (data.payment > 0) {
                    showSuccessModal(data);
                } else {
                    showUploadResult('error', '上传失败', data.message);
                }

            } catch (error) {
                isUploading = false;
                showUploadResult('error', '上传出错', error.message);
            } finally {
                updateUploadButtonState();
                hideProgress();
            }
        }

        function updateUploadButtonState() {
            if (isUploading) {
                uploadBtn.innerHTML = '<span class="icon icon-spinner"></span> 上传中...';
                uploadBtn.disabled = true;
            } else {
                uploadBtn.innerHTML = '<span class="icon icon-upload"></span> 选择文件';
                uploadBtn.disabled = false;
            }
        }

        function showProgress() {
            progressContainer.style.display = 'block';
        }

        function hideProgress() {
            progressContainer.style.display = 'none';
        }

        function updateProgress(progress, processed, total) {
            progressFill.style.width = progress + '%';
            progressText.innerHTML = `<span class="icon icon-spinner"></span> 上传中... ${Math.round(progress)}%`;
            progressInfo.textContent = `已处理: ${processed}/${total}`;
            
            if (estimatedTime) {
                estimatedTimeEl.textContent = `预计剩余时间: ${estimatedTime}`;
            }
        }

        function showUploadResult(type, text, details = '') {
            resultSection.style.display = 'block';
            resultTitle.className = `result-title ${type}`;
            
            const icon = type === 'success' ? 'icon-success' : 'icon-error';
            resultTitle.innerHTML = `<span class="icon ${icon}"></span><span>${text}</span>`;
            
            if (details) {
                resultDetails.textContent = details;
                resultDetails.style.display = 'block';
            } else {
                resultDetails.style.display = 'none';
            }
        }

        function hideUploadResult() {
            resultSection.style.display = 'none';
        }

        async function searchOrder() {
            const orderNumber = searchInput.value.trim();
            if (!orderNumber) {
                alert('请输入订单号');
                return;
            }

            isSearching = true;
            updateSearchButtonState();
            hideSearchResult();

            try {
                const response = await fetch(`/all/default-${orderNumber}`);
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }

                const data = await response.json();
                showSearchResult();

                if (data.payment === -1) {
                    showSearchError('未找到该订单编号对应的数据');
                } else if (data.payment === -2) {
                    showSearchError('查询失败');
                } else {
                    showSearchSuccess(data);
                }
            } catch (error) {
                showSearchError('查询失败：' + error.message);
            } finally {
                isSearching = false;
                updateSearchButtonState();
            }
        }

        function updateSearchButtonState() {
            if (isSearching) {
                searchBtn.innerHTML = '<span class="icon icon-spinner"></span> 检索中...';
                searchBtn.disabled = true;
            } else {
                searchBtn.innerHTML = '<span class="icon icon-search"></span> 检索';
                searchBtn.disabled = false;
            }
        }

        function showSearchResult() {
            priceDisplay.style.display = 'block';
        }

        function hideSearchResult() {
            priceDisplay.style.display = 'none';
        }

        function showSearchSuccess(data) {
            searchResultContent.innerHTML = `
                <div style="display: flex; gap: 20px; flex-wrap: wrap;">
                    <div style="flex: 1; min-width: 200px;">
                        <strong>订单编号：</strong>${data.order_number}<br>
                        <strong>上传时间：</strong>${formatTime(data.upload_time)}
                    </div>
                    <div style="flex: 1; min-width: 200px;">
                        <div class="price-value">金额：¥${data.payment}</div>
                    </div>
                </div>
            `;
        }

        function showSearchError(message) {
            searchResultContent.innerHTML = `<div class="error-message">${message}</div>`;
        }

        function formatTime(timeString) {
            if (!timeString) return '未知时间';
            const date = new Date(timeString);
            return date.toLocaleString('zh-CN');
        }

        function showSuccessModal(data) {
            // 更新上传结果
            showUploadResult('success', '上传成功！', data.message);

            // 更新模态框内容
            modalBody.innerHTML = `
                <div class="upload-stats">
                    <div class="stat-item">
                        <div class="stat-number">${data.total || 0}</div>
                        <div class="stat-label">总记录数</div>
                    </div>
                    <div class="stat-item">
                        <div class="stat-number">${data.success || 0}</div>
                        <div class="stat-label">成功</div>
                    </div>
                    <div class="stat-item">
                        <div class="stat-number">${data.error || 0}</div>
                        <div class="stat-label">失败</div>
                    </div>
                </div>
                <div class="success-stat">
                    <div class="stat-number">${data.payment || 0}</div>
                    <div class="stat-label">总金额</div>
                </div>
                ${data.message ? `<p style="margin-top: 15px;">${data.message}</p>` : ''}
            `;

            // 显示成功弹窗
            successModal.style.display = 'flex';
        }

        function closeModal() {
            successModal.style.display = 'none';
        }
    </script>
</body>
</html> 